﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Data.OleDb;
using MM.DBUtility;

namespace MobileManager
{
    public partial class InfoManager : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
                if (userModel.RoleID.Equals("3"))
                    return;
            }
            if (!IsPostBack)
            {
                try
                {
                    GVDataBind();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        private void GVDataBind()
        {
            DataSet ds = DbHelperSql.Query("SELECT * FROM T_MobileInfo WITH(NOLOCK) ");
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
            {
                string strStatus = ((DataRowView)e.Row.DataItem)["Status"].ToString().ToLower();
                Label lbStatus = (Label)e.Row.FindControl("lbStatus");
                if (strStatus.Equals("true"))
                    lbStatus.Text = "启用";
                else lbStatus.Text = "禁用";
            }
        }

        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVDataBind();
                string strStatus = ((HiddenField)GVinfo.Rows[e.NewEditIndex].FindControl("hdfStatus")).Value.ToLower();
                DropDownList ddlStatus = ((DropDownList)GVinfo.Rows[e.NewEditIndex].FindControl("ddlStatus"));
                if (strStatus.Equals("true"))
                    ddlStatus.SelectedValue = "1";
                else ddlStatus.SelectedValue = "2";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;
                string strTarget = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("hdfTarget")).Value;
                string strContent = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("hdfContent")).Value;
                DbHelperSql.ExecuteSql(string.Format("DELETE FROM T_MobileInfo WHERE ID={0}", strID));
                GVDataBind();
                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("删除了短信：发送目标={0}，发送内容={1}。", strTarget, strContent), "短信管理");
                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strTarget = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtTarget")).Text.Trim();
                string strContent = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtContent")).Text.Trim();
                string strStatus = ((DropDownList)GVinfo.Rows[e.RowIndex].FindControl("ddlStatus")).SelectedValue;
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;
                if (string.IsNullOrEmpty(strTarget))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "发送目标不能为空！");
                    return;
                }
                if (string.IsNullOrEmpty(strContent))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "发送内容不能为空！");
                    return;
                }
                MM.Model.T_MobileInfo miModel = new MM.Model.T_MobileInfo();
                miModel.ID = strID;
                miModel.Target = strTarget;
                miModel.Content = strContent;
                if (strStatus.Equals("1"))
                    miModel.Status = MM.Model.T_MobileInfo.StatusEnum.Enabled;
                else miModel.Status = MM.Model.T_MobileInfo.StatusEnum.Disabled;
                MM.DAL.T_MobileInfo miDAL = new MM.DAL.T_MobileInfo();
                miDAL.Update(miModel);

                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("更新了短信：发送目标={0}，发送内容={1}，状态={2}。", strTarget, strContent, strStatus), "短信管理");
                MM.Common.Common.PageStartAlert(this.Page, "更新成功！");

                //DbHelperSql.ExecuteSql("UPDATE T_MobileInfo SET Content='test111',Target='111',Status=true WHERE ID=7");
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strTarget = txtTarget.Text.Trim();
                if (string.IsNullOrEmpty(strTarget))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "发送目标不能为空！");
                    return;
                }
                string strContent = txtContent.Text.Trim();
                if (string.IsNullOrEmpty(strContent))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "发送内容不能为空！");
                    return;
                }
                string strStatus = ddlStatus.SelectedValue;
                MM.Model.T_MobileInfo miModel = new MM.Model.T_MobileInfo();
                miModel.Target = strTarget;
                miModel.Content = strContent;
                if (strStatus.Equals("1"))
                    miModel.Status = MM.Model.T_MobileInfo.StatusEnum.Enabled;
                else miModel.Status = MM.Model.T_MobileInfo.StatusEnum.Disabled;

                MM.DAL.T_MobileInfo miDAL = new MM.DAL.T_MobileInfo();
                miDAL.Add(miModel);
                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("添加了短信：发送目标={0}，发送内容={1}，状态={2}。", strTarget, strContent, strStatus), "短信管理");
                MM.Common.Common.PageStartAlert(this.Page, "添加成功！");
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
